/**
 * @author JiuQi 玖柒
 * @date 2021/7/5 23:38
 */
public class T35 {

    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 7, 8, 10};
        int target = 5;
        System.out.println(new Solution1().searchInsert(nums, target));
    }
}

//class Solution1 {
//    public int searchInsert(int[] nums, int target) {
//        int left = 0, right = nums.length - 1;
//        if (target < nums[left]) {
//            return 0;
//        } else if (target > nums[right]) {
//            return nums.length;
//        }
//        while (left <= right) {
//            int mid = left + (right - left) / 2;
//            if (nums[mid] < target) {
//                left = mid + 1;
//            } else if (nums[mid] > target) {
//                right = mid - 1;
//            }
//        }
//        return left;
//    }
//}

class Solution1 {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        int mid = 0;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }
}
